package it.asvp.asvp_portal.data.dao.impl;


import it.asvp.asvp_portal.data.dao.MemberAccountRoleDao;
import it.asvp.asvp_portal.data.entity.member.MemberAccount;
import it.asvp.asvp_portal.data.entity.member.MemberAccountRole;
import it.asvp.asvp_portal.data.entity.member.Role;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class MemberAccountRoleDaoImpl implements MemberAccountRoleDao{

	private static final Log log = LogFactory.getLog(MemberAccountRoleDao.class);

	@Autowired
	SessionFactory sessionFactory;

	public Long persist(MemberAccountRole transientInstance) {
		log.debug("persisting MemberAccountRole instance");
		Long id;
		try {
			id=(Long) sessionFactory.getCurrentSession().save(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
		return id;
	}

	public void delete(MemberAccountRole persistentInstance) {
		log.debug("deleting MemberAccountRole instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public MemberAccountRole merge(MemberAccountRole detachedInstance) {
		log.debug("merging MemberAccountRole instance");
		try {
			MemberAccountRole result = (MemberAccountRole) sessionFactory.getCurrentSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

    public MemberAccountRole findById(java.lang.Long id) {
		
		Criteria cr=sessionFactory.getCurrentSession().createCriteria(MemberAccountRole.class);
		cr.add(Restrictions.eq("id", id));
		MemberAccountRole mAR = (MemberAccountRole)cr.uniqueResult();
		return mAR;
	}
    
    public MemberAccountRole findBy(MemberAccount account,Role role) {
		
		Criteria cr=sessionFactory.getCurrentSession().createCriteria(MemberAccountRole.class);
		cr.add(Restrictions.eq("memberAccount", account));
		cr.add(Restrictions.eq("role", role));
		MemberAccountRole mAR = (MemberAccountRole)cr.uniqueResult();
		return mAR;
	}
    
   
	
}
